package com.dcbeida.activity.msg;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.MsgListAdapter;
import com.dcbeida.bean.MsgBean;
import com.dcbeida.utils.SharedPreferencesUtils;
import com.dcbeida.widget.DataLoadDirection;
import com.dcbeida.widget.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 消息
 * Sean
 */

public class MsgActivity extends BaseActivity implements XListView.IXListViewListener, MsgContract.View {
    @Bind(R.id.img_back)
    ImageView img_back;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.myList)
    XListView myList;
    @Bind(R.id.ll_no_data)
    LinearLayout ll_no_data;


    private boolean isEdit = false;
    private boolean isEnd = false;// 是否有更多
    private int Totle = 0;//
    private int pageindex = 0;
    private int pagesize = 10;

    int type = DataLoadDirection.Refresh;

    private List<MsgBean> datalist;
    private MsgListAdapter adapter;

    private MsgPresenter mPresent;


    @Override
    protected int getLayoutResId() {
        return R.layout.msg_listview;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initParams();
        initData();

        SharedPreferencesUtils sp = SharedPreferencesUtils.getInstance(this);
        sp.setIsHidden(true);//消息已读

        mPresent = new MsgPresenter(this, this);
        mPresent.getList(pageindex, pagesize);

    }

    @OnClick(R.id.img_back)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }

    private void initParams() {
        tvTitle.setText("通知");
    }

    private void initData() {
        datalist = new ArrayList<MsgBean>();

        myList.setPullLoadEnable(false);
        myList.setPullRefreshEnable(true);
        myList.setXListViewListener(this);
        // actualListView.setColumnNumber(1);
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条

        adapter = new MsgListAdapter(this);
        myList.setAdapter(adapter);

        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                if(position>0){
                    bundle.putSerializable("msgbean",adapter.getmArrayList().get(position-1));
                }
                showActivity(MessageDetailActivity.class,bundle);
            }
        });
    }

    @Override
    public void showTelTip(String msg) {
        showTip(msg);
    }

    @Override
    public void showList(ArrayList<MsgBean> list) {
        if (list.size() >= 0) {

            if (list.size() < 10)
                isEnd = false;
            else
                isEnd = true;

            Totle = adapter.getmArrayList().size() + list.size();

//            List<MsgBean> msgBeanList = new ArrayList<>();
//            for (int i=0;i<20;i++){
//                MsgBean message = new MsgBean();
//                message.setMsgcontent(list.get(i%list.size()).getMsgcontent());
//                message.setMsgtitle(list.get(i%list.size()).getMsgtitle());
//                msgBeanList.add(message);
//            }
//
//            list.addAll(msgBeanList);
            if (type == DataLoadDirection.Refresh) {
                myList.stopRefresh();
                pageindex = 0;
                adapter.clearList();
            } else if (type == DataLoadDirection.LoadMore) {
                myList.stopLoadMore();
                pageindex++;
            }

            if (isEnd) {
                myList.showFooterView();
                myList.setPullLoadEnable(true);
            } else {
                myList.hideFooterView();
                myList.setPullLoadEnable(false);
            }

            adapter.setmArrayList(list);
        }

        if (0 == adapter.getCount()) {
            myList.setVisibility(View.GONE);
            ll_no_data.setVisibility(View.VISIBLE);
        } else {
            myList.setVisibility(View.VISIBLE);
            ll_no_data.setVisibility(View.GONE);
        }


    }

    @Override
    public void setPresenter(MsgContract.Present presenter) {

    }


    @Override
    public void onRefresh() {
        // 执行刷新任务
        type = DataLoadDirection.Refresh;
        mPresent.getList(0, pagesize);
    }

    @Override
    public void onLoadMore() {
        // 执行加载更多任务
        type = DataLoadDirection.LoadMore;
        if (isEnd) {
            mPresent.getList(pageindex + 1, pagesize);
        }
    }
}
